const WebSocket = require ('ws')
const http = require('http')
const { client } = require('webpack-dev-server/bin/cli-flags')

const server = http.createServer()
const wss = new WebSocket.WebSocketServer({
   noServer: true
})


wss.on('connection', function connection(ws) {
    console.log("触发 wss 的 connection")
    ws.on('message', function message(data){
        console.log("服务端收到客户端的消息", JSON.parse(data))
    })

    ws.send("222")
})


server.on('upgrade', function upgrade(request, socket, head){
    console.log("触发server的upgrade")
    wss.handleUpgrade(request, socket, head, function done(ws){
        wss.emit('connection', ws, request, client)
    })
})

server.listen(9000)

